<?php
require "../../sesi_login.php";
require "../../konfigurasi.php";

if(isset($_POST['pegawai']) && $_POST['pegawai']=="tambah") {
	koneksi_buka();
	
	$nip		= $_POST['nip'];
	$nama		= strtoupper($_POST['nama']);
	$jabatan	= $_POST['jabatan'];
	$pangkat	= $_POST['pangkat'];
	$alamat		= $_POST['alamat'];
	$telepon	= $_POST['telepon'];
	$handphone	= $_POST['handphone'];
	
	$username	= $_POST['username'];
	$password	= $_POST['password'];
	$password2	= $_POST['password2'];
	
	if(strlen($username)==0 && $nama!="" && $alamat!="" && $handphone!="" ) {
		mysql_query("INSERT INTO t_pegawai VALUES('','$nip','$jabatan','$pangkat','0','$nama','$alamat','$telephone','$handphone')");
		header("Location:../../pegawai.php");
	} else {
		$user_ada = mysql_num_rows(mysql_query("SELECT * FROM t_pengguna WHERE username='".$username."'"));
		if($user_ada==1) {
			header("Location:../../pegawai.php?tambah");
		} else {
			if($password != $password2) {
				header("Location:../../pegawai.php?tambah");
			} else {
				mysql_query("INSERT INTO t_pengguna VALUES('','$username','".md5($password)."','pegawai')");
				$kd_pengguna = mysql_fetch_array(mysql_query("SELECT kd_pengguna FROM t_pengguna WHERE username='".$username."'"));
				mysql_query("INSERT INTO t_pegawai VALUES('','$nip','$jabatan','$pangkat','".$kd_pengguna[0]."','$nama','$alamat','$telephone','$handphone')");
				header("Location:../../pegawai.php");
			}
		}
	}
	
	koneksi_tutup();
} elseif(isset($_POST['pegawai']) && $_POST['data']=="ubah") {
	koneksi_buka();
	
	$nip		= $_POST['nip'];
	$nama		= strtoupper($_POST['nama']);
	$jabatan	= $_POST['jabatan'];
	$pangkat	= $_POST['pangkat'];
	$alamat		= $_POST['alamat'];
	$telepon	= $_POST['telepon'];
	$handphone	= $_POST['handphone'];
	
	$username	= $_POST['username'];
	$password	= $_POST['password'];
	$password2	= $_POST['password2'];
	
	echo "ubah";
	
	koneksi_tutup();
} elseif(isset($_GET['kd']) && $_GET['kd']!="") {
	koneksi_buka();
	mysql_query("DELETE FROM t_jaksa WHERE kd='".$_GET['kd']."'");
	header("Location:../../pegawai.php");
	koneksi_tutup();
} else {
	header("Location:../../pegawai.php");
}
?>
